

import os
from PIL import Image, ImageTk
from tkinter import Tk, Label, Button, Frame


def load_images(directory):
    images = {}
    for filename in os.listdir(directory):
        if filename.endswith(".png"):
            img = Image.open(os.path.join(directory, filename))
            # root = Tk()
            tkimg = ImageTk.PhotoImage(img)
            images[filename] = tkimg
    return images


def show_images(image_dict):
    root = Tk()
    root.title("Image Viewer")
    frame = Frame(root)
    frame.pack()
    current_image_index = 0
    for index, (filename, img) in enumerate(image_dict.items()):
        Label(frame, image=img).pack()  # 在这里我们使用了一个额外的循环来将图片并排显示
        Label(frame, text=f"Folder Name {index + 1}").pack()  # 同样地，我们在这里添加了一个标签来显示文件夹名称
    button_previous = Button(frame, text="Previous",
                             command=lambda: show_previous_image(current_image_index, image_dict))
    button_previous.pack()
    button_next = Button(frame, text="Next", command=lambda: show_next_image(current_image_index, image_dict))
    button_next.pack()
    root.mainloop()


def show_previous_image(current_image_index, image_dict):
    current_image_index -= 1
    if current_image_index < 0:
        current_image_index = len(image_dict) - 1
    show_images(
        {k: v for k, v in image_dict.items() if k != list(image_dict.keys())[current_image_index]})  # 创建一个新的窗口显示当前图片


def show_next_image(current_image_index, image_dict):
    current_image_index += 1
    if current_image_index >= len(image_dict):
        current_image_index = 0
    show_images(
        {k: v for k, v in image_dict.items() if k != list(image_dict.keys())[current_image_index]})  # 创建一个新的窗口显示当前图片


# 预设文件夹和图片信息，你需要替换为实际的路径和文件名
directories = ['/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/LGFO_Net/predictResult',
               '/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/LiteST_Net/predictResult',
               '/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/NestedUNet/predictResult',
               '/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/SegNet/predictResult',
               '/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/TVResNet50_PSPNet64/predictResult'
               ]  # 文件夹路径列表
# 文件夹路径列表
image_dict = load_images('/home/jolly/develop/PycharmProjects/remote_sensor/bijie256/LGFO_Net/predictResult')  # 加载文件夹中的图片列表，这里我们假设文件夹A中包含所有需要的图片文件
if len(image_dict) > 0:  # 如果字典不为空，显示第一张图片和对应的文件夹名称
    show_images(image_dict)